package xiao.trip.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import xiao.trip.Do.shopDO;
import xiao.trip.dao.shopDAO;

import javax.servlet.http.HttpServletRequest;

@Controller
public class shopC {
    @Autowired
    shopDAO shop;

    @RequestMapping("/shop")
    public String shop_ex(HttpServletRequest request , Model model, shopDO shopD){
        String shopTitle=shopD.getShopTitle();
        String shopId=shopD.getShopId();
        Double lowPrice=shopD.getLowPrice();
        String trait=shopD.getTrait();
        String username=shopD.getUsername();
        String userphone=shopD.getUserphone();
        int count=shopD.getCount();
        String hp=shopD.getHp();
        if (shopId!=null&&shop.select_Id(shopId).size()==0){
            hp="../static/img/" + (int)Math.floor(Math.random()*10) +".jpg";
            shop.insert_all(shopTitle,trait,lowPrice,username,userphone,shopId,hp,count);
        }

        String id=request.getParameter("shopId");
        if (id!=null){
            shop.update_count(id);
        }
        if (shop.select_count().size()!=0) {
            for (shopDO a:shop.select_count()){
                shop.delete_Id(a.getShopId());
            }
        }
        model.addAttribute("shop_list",shop.select_all());
        return "user/shop";
    }

}
